﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseFunction;
using BaseFunction.StaticTableClass;
using System.Web.Security;

namespace YY.Web
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtUserName.Focus();

                if (!Page.IsPostBack)
                {
                    txtUserName.Text = Utils.GetCookie("YYAgentRememberName");
                    InitData();
                }
            }

        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.Page.Title = SystemConfigHelp.GetSystemConfigValue("mallname") + " - " + SystemConfigHelp.GetSystemConfigValue("companyname");
            ltlfootInfo.Text = this.Page.Title.Split('-')[1];
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var getModel = new YY.Model.agent_login_log();
            /*验证用户名和密码*/
            var userName = txtUserName.Text.Trim();
            var pwd = txtPassword.Text.Trim();
            if (userName == "")
            {
                msgtip.InnerHtml = "用户名不允许为空！";
                return;
            }
            else if (pwd == "")
            {
                msgtip.InnerHtml = "密码不允许为空！";
                return;
            }
            else
            {
                string strWhere = string.Format(@" 1=1 AND user_name='{0}' OR mobile='{1}' OR e_mail='{2}'", userName, userName, userName);

                var dt = BaseFunction.StaticTableClass.agent_users_Help.bll_agent_users.GetList(strWhere).Tables[0];
                if (dt.Rows.Count == 0)
                {
                    msgtip.InnerHtml = "账号不存在！";
                    return;
                }
                else
                {
                    int count = 0;
                    string rightPwd = dt.Rows[0]["password"].ToString();
                    string isLock = dt.Rows[0]["is_lock"].ToString();
                    int keyId = int.Parse(dt.Rows[0]["id"].ToString());
                    if (isLock == "1")//账号锁定
                    {
                        MessageBox.Show(this.Page, "此账号已经被锁定！请和系统管理员联系!");
                        Session.Clear();
                        Session.Abandon();
                        Request.Cookies.Clear();
                        FormsAuthentication.SignOut();
                        return;
                    }
                    else
                    {
                        var model = new agent_users_Help().GetModel(userName, pwd, true);//判断是否存在该用户的model
                        if (model == null)
                        {
                            //判断输错密码的次数 
                            if (Request.Cookies["loginnum"] == null)
                            {    //写入Cookies
                                Utils.WriteCookie("loginnum", "1", 60);
                            }
                            else
                            {
                                //累加一次错误
                                count = int.Parse(Utils.GetCookie("loginnum"));
                                int num = count + 1;
                                Utils.WriteCookie("loginnum", num.ToString());
                            }
                            //判断登录错误次数
                            if (int.Parse(Utils.GetCookie("loginnum")) > 5)
                            {
                                MessageBox.Show(this.Page, "密码连续错误5次，禁止登录!!!");
                            }
                            msgtip.InnerHtml = "密码不正确！";
                            return;
                        }
                        if (Request.Cookies["loginnum"] == null)
                        {
                            //.
                        }
                        else
                        {
                            if (int.Parse(Utils.GetCookie("loginnum")) > 5)
                            {
                                MessageBox.Show(this.Page, "密码连续错误5次，禁止登录!!!");
                                return;
                            }
                        }

                        /*验证成功 赋值到session  并且跳转*/
                        Session["AgentId"] = keyId;
                        /*验证成功后,写日志*/
                        getModel.user_id = keyId;
                        getModel.user_name = userName;
                        getModel.action_type = YYEnums.ActionEnum.Login.ToString();//操作类型 login
                        getModel.add_time = DateTime.Now;//操作时间
                        getModel.remark = "于" + RequestValidate.GetIP() + " 登陆成功。";
                        getModel.login_ip = RequestValidate.GetIP();//登陆当前计算机IP
                        //写入登录日志
                        agent_login_log_Help.bll_agent_login_log.Add(getModel);

                        //写入Cookies
                        Utils.WriteCookie("YYAgentRememberName", model.user_name, 14400);
                        Utils.WriteCookie("YYAgentName", "YYAgent", model.user_name);
                        Utils.WriteCookie("YYAgentPassword", "YYAgent", model.password);
                        Response.Redirect("Default.aspx");
                        return;
                    }
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            txtUserName.Text = "15312081796";
            txtPassword.Text = "111111";
            btnSubmit_Click(null,null);
        }
    }
}